package sp.whereru;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class NavigationView extends View {

	private Float direction;
	private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
	private GPS gps;
	
	public NavigationView(Context context) {
		super(context);
		init();
	}
	
	public NavigationView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	public NavigationView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}
	
	private void init() {	
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(3);
		paint.setColor(Color.WHITE);
		paint.setTextSize(30);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
	}	
	
	@Override		
	protected void onDraw(Canvas canvas) {
	      int width = getMeasuredWidth();
	      int height = getMeasuredHeight();
	      int cxCompass = width/2;
	      int cyCompass = height/2;			
	      float radius = (cxCompass > cyCompass)? cyCompass:cxCompass;
	      paint.setColor(Color.WHITE);
		  canvas.drawCircle(cxCompass, cyCompass, radius, paint);
	      canvas.drawLine(cxCompass, 0, cxCompass, height, paint);
	      canvas.drawLine(0, cyCompass, width, cyCompass, paint);     
	      if (direction != null)
	    	  canvas.rotate((float) Math.toDegrees(direction) + (float) gps.angleToNorth(39.721273d, -75.132108d) , 
	    			  cxCompass, cyCompass);
	      paint.setColor(Color.RED);
		  canvas.drawLine(cxCompass, cyCompass, cxCompass, 1, paint);
		  canvas.drawText("Next", cxCompass+5, cyCompass-10, paint);
	}
	
	public void updateDirection(float dir, GPS gps){
		direction = dir;
		this.gps = gps;
		invalidate();
	}
}
